<?php

namespace App\Console\Commands;

use Facebook\WebDriver\WebDriverBy;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;

class webtest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'webtest';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        set_time_limit(0);
        $driver = RemoteWebDriver::create(
            'http://localhost:9515', DesiredCapabilities::chrome()
        );
        $driver->manage()->timeouts()->pageLoadTimeout(300)->implicitlyWait(300)->setScriptTimeout(300);
        $driver->executor->setRequestTimeout(500000);
        $driver->executor->setConnectionTimeout(500000);
        $driver->get('https://trade.foundersc.com/Fzwt_login.aspx');
//        sleep(60);
        $driver->switchTo()->alert()->accept();

        $cookie = $driver->manage()->getCookies();
        $img = $driver->findElement(WebDriverBy::id('img1'))->getAttribute('src');

        $driver->findElement(WebDriverBy::id('img3'))->click();
        $driver->findElement(WebDriverBy::id('Text1'))->sendKeys('66064265');
        $driver->findElement(WebDriverBy::id('Text2'))->sendKeys('890890');
//        $driver->executeScript("window.open(\"$img\");");
//        $handles = $driver->getWindowHandles();
//        $driver->switchTo()->window($handles[1]);
//        $driver->takeScreenshot('./test.png');
//        $code = getCheckCodeByNumOfTime('./test.png');
//        sleep(1);
        $client = new Client();
        $code = $client->get('http://www.zidongdaxin.com/admin/test/getVerifyMiddle/?cookie_str='.$cookie[0]['value'])->getBody()->getContents();
//        $code = $client->get('http://zddx.app/admin/test/getVerifyMiddle/?cookie_str='.$cookie[0]['value'])->getBody()->getContents();
        dump($cookie[0]['value']);
        dump($code);
        $driver->findElement(WebDriverBy::id('Text3'))->sendKeys($code);
        sleep(2);
//        $driver->executeScript("ckurl();");
        $driver->findElement(WebDriverBy::id('hqjybtn'))->click();
        sleep(20);
        $handles = $driver->getWindowHandles();
        dump($handles);
        dump($driver->manage()->getCookies());
        $t = $driver->findElements(WebDriverBy::className("fontchu"))[2]->click();
//        dump($t);
//        $driver->findElement(WebDriverBy::linkText('交易'))->click();
////        $driver->executeScript("changeMain('3');");
//        sleep(2);
//        $driver->findElement(WebDriverBy::id('zqdm'))->sendKeys('300636');
//        sleep(2);
//        $driver->findElement(WebDriverBy::id('Button2'))->click();
//        $driver->close();
    }
}
